package com.lqs.blog.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lqs.blog.mapper.SysUserMapper;
import com.lqs.blog.pojo.SysUser;
import com.lqs.blog.service.LoginService;
import com.lqs.blog.service.SysUserService;
import com.lqs.blog.vo.ErrorCode;
import com.lqs.blog.vo.LoginUserVo;
import com.lqs.blog.vo.Result;
import com.lqs.blog.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SysUserServiceImpl implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private LoginService loginService;

    @Override
    public SysUser getUserById(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        System.out.println(sysUser);
        if (sysUser == null) {
            sysUser = new SysUser();
            sysUser.setNickname("老罗");
        }
        return sysUser;
    }

    @Override
    public SysUser getUserByAccountAndPassword(String account, String password) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount, account).eq(SysUser::getPassword, password);
        wrapper.select(SysUser::getNickname, SysUser::getAvatar, SysUser::getId, SysUser::getAccount);
        wrapper.last("limit 1");//查询一条数据
        return sysUserMapper.selectOne(wrapper);
    }

    @Override
    public Result getUserByToken(String token) {
        /*1.token合法性校验
        是否为空 是否解析成功 在redis中是否存在
        2.如果效验失败 返回错误
        3.如果成功 返回对应结果 LoginUserVo
        *
        * */
        SysUser sysUser = loginService.checkToken(token);
        if (sysUser == null) {
            return Result.fail(ErrorCode.TOKEN_ERROR.getCode(), ErrorCode.TOKEN_ERROR.getMsg());
        }
        LoginUserVo loginUserVo = new LoginUserVo();
        loginUserVo.setId(sysUser.getId());
        loginUserVo.setAccount(sysUser.getAccount());
        loginUserVo.setAvatar(sysUser.getAvatar());
        loginUserVo.setNickName(sysUser.getNickname());
        return Result.success(loginUserVo);
    }

    @Override
    public SysUser getUserByAccount(String account) {
        LambdaQueryWrapper<SysUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysUser::getAccount, account);
        wrapper.select(SysUser::getNickname, SysUser::getAvatar, SysUser::getId, SysUser::getAccount);
        wrapper.last("limit 1");//查询一条数据,提高效率
        return sysUserMapper.selectOne(wrapper);
    }

    @Override
    public void save(SysUser sysUser) {
        //保存用户这 id会自动生成
        //这个地方 默认生成的id是 分布式id 雪花算法
        //mybatis-plus
        sysUserMapper.insert(sysUser);
    }

    @Override
    public UserVo getUserVoById(Long authorId) {
        SysUser sysUser = sysUserMapper.selectById(authorId);
        UserVo userVo = new UserVo();
        userVo.setId(sysUser.getId().toString());
        BeanUtils.copyProperties(sysUser, userVo);
        return userVo;
    }

}
